// 实现这个项目的构建任务
const {src,dest,parallel,series,watch} = require("gulp");
const plugins = require('gulp-load-plugins')();
const borwserSync = require('browser-sync') 
const bs = borwserSync.create()//自动打开浏览器
const data = {
    //模拟模板文件里的数据
    menus: [
      {
        name: 'Home',
        icon: 'aperture',
        link: 'index.html'
      },
      {
        name: 'Features',
        link: 'features.html'
      },
      {
        name: 'About',
        link: 'about.html'
      },
      {
        name: 'Contact',
        link: '#',
        children: [
          {
            name: 'Twitter',
            link: 'https://twitter.com/w_zce'
          },
          {
            name: 'About',
            link: 'https://weibo.com/zceme'
          },
          {
            name: 'divider'
          },
          {
            name: 'About',
            link: 'https://github.com/zce'
          }
        ]
      }
    ],
    pkg: require('./package.json'),
    date: new Date()
  }

const style = () =>{
    return src('src/assets/styles/*.scss',{base: 'src'})
    .pipe(plugins.sass({outputStyle: 'expanded'}))
    .pipe(dest('temp'))
}

const scripts = () =>{
    return src('src/assets/scripts/*.js',{base: 'src'})
    .pipe(plugins.babel({presets: ['@babel/env']}))
    .pipe(dest('temp'))
}

const page = () =>{
    return src('src/*.html',{base: 'src'})
    .pipe(plugins.swig(data))
    .pipe(dest('temp'))
}

const img = () =>{
    return src('src/assets/images/**',{base: 'src'}).pipe(plugins.cache(
        plugins.imagemin({
            progressive : true,//是否渐进的优化
            svgoPlugins : [{removeViewBox:false}],//svgo插件是否删除幻灯片
            interlaced : true //是否各行扫描
        })
    )).pipe(dest('dist'))
}

const font = () => {
    return src('src/assets/fonts/**',{base: 'src'})
    .pipe(plugins.imagemin())
    .pipe(dest('dist'))
}

const extra = () =>{
    return src('public/**',{base: 'public'})
    .pipe(dest('dist'))
}

const clean = () =>{
    return src(['dist','temp'])
    .pipe(plugins.clean())
}
const useref = () =>{
    return src('temp/*.html',{base: 'temp'}).pipe(plugins.useref({searchPath:['temp','.'] }))
            .pipe(plugins.if(/\.js$/, plugins.uglify()))
            .pipe(plugins.if(/\.css$/, plugins.cleanCss()))
            .pipe(plugins.if(/\.html$/, plugins.htmlmin({ 
                collapseWhitespace: true,
                minifyCSS: true, //页面里的style和js脚本压缩
                minifyJS: true
            }))) //html    
           .pipe(dest('dist')) 
}

const serve = () => {  
    watch('src/assets/styles/*.scss', style)
    watch('src/assets/scripts/*.js', scripts)
    watch('src/*.html', page)
    watch([
      'src/assets/images/**',
      'src/assets/fonts/**',
      'public/**'
    ], bs.reload)

    bs.init({
      notify: false,
      port: 3000,
      files:'temp/**',//监听dist文件下有改动的时候刷新页面
      server: {
        baseDir: ['temp','src','public'],
        routes: {
          '/node_modules': 'node_modules' //自动映射到项目下的node_modules
        }
      }
    })
}
const compile =  parallel([style,scripts,page]);

const build = series(clean, parallel(series(compile, useref),extra, img, font))

const develop = series(compile,serve)

module.exports = {
    clean,
    build,
    develop
}